<?php

namespace app\admin\controller;

use app\admin\model\Occu;
use think\Db;
use think\Request;
use think\Config;

use app\admin\model\Authen as AuthenModel;

class AuthenController extends BaseController{
    //列表
    public function lst(){
        $map = [];
        $params=$this->request->param();
        if(!empty($params['occuption']) )
        {
            $map['occuption']=array('like','%'.$params['occuption'].'%');
        }else{
            $map=[];
        }

        $data =AuthenModel::where('delete_time',null)
            ->where('status',1)
            ->where($map)
            ->paginate(5);
          $occu =   Occu::all();
        $this->assign([
            'data'=>$data,
            'occu'=>$occu,
        ]);
        return $this->fetch('authen/lst');
    }
    //添加
    public function add(){
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                $cates = Config::get('authen.channel');
                $occu = Occu::all();
                $this->assign([
                'cates'=> $cates,
                    'occu'=>$occu,
                ]);
                return $this->fetch('authen/add');
                break;

            case "POST":
                $params = $request->param();
                $controller = strtolower($request->controller());//生成控制器名字文件夹
                if(request()->file('pic')){
                    $picInfo = uploadOne($controller,'thumb','pic',150,150);
                    $params['pic'] = $picInfo['pic'];
                    $params['sm_pic'] = $picInfo['thumb_pic'];
                }
                $model = new AuthenModel();
                if($model->validate(true)->allowField(true)->save($params)){
                    $this->success('添加成功',url('admin/authen/lst'),'',1);
                }else{
                    $this->error($model->getError() );
                }
        }
        return '错误信息';
    }

    //删除
    public function del()
    {
        $id = $this->request->param("id");
        $model = AuthenModel::find($id);
        if(is_file($model->pic)){
            unlink($model->pic);
        }
        if(is_file($model->sm_pic)){
            unlink($model->sm_pic);
        }
        if(AuthenModel::destroy($id)){
            return '亲,您已删除成功!^o^';
        }
        return '删除失败...';
    }

    //修改
    public function edit(){
        $request = Request::instance();
        if(request()->isGet()){
            $id = $this->request->param('id');
            $data = AuthenModel::find($id);
            //var_dump($data);die;
            $occu = Occu::all();
            $this->assign([
                'cates' => Config::get('authen.channel'),
                'data' => $data,
                'occu'=>$occu,
            ]);
            return $this->fetch('authen/edit');
        }

        if(request()->isPost()) {
            $params = $request->param();
           // $params = $this->request->param();
           // $id = $this->request->param('id');
            $model = new AuthenModel();
            if(request()->file('pic')) {
                $controller = strtolower($request->controller());
                $picInfo = uploadOne($controller, 'thumb', 'pic', 150, 150);
                $params['pic'] = $picInfo['pic'];
                $params['sm_pic'] = $picInfo['thumb_pic'];

                $oldPic = $model->where('id',$params['id'])->find();
                if (is_file($oldPic['sm_pic'])) {
                    @unlink($oldPic['sm_pic']);
                }
                if (is_file($oldPic['pic'])) {
                    @unlink($oldPic['pic']);
                }
        }
            if($model->validate(true)->allowField(true)->isUpdate(true)->save($params)){
                $this->success('修改成功',url('admin/authen/lst'),'',1);
            }else{
                $this->error($model->getError() );
            }
        }
        return '错误信息';
    }

}